package com.huashi.dealer.modular.controller.api.upload;

import com.huashi.dealer.commonEntity.UploadFile;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.upload.service.UploadService;
import com.huashi.dealer.modular.business.upload.param.UploadResponseParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * API上传控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API上传控制器")
@RestController("apiUploadController")
@RequestMapping("/api/upload")
@Validated
public class UploadController {

    @Resource
    private UploadService uploadService;

    /**
     * 上传图片
     */
    @PostMapping("/image")
    @Operation(summary = "上传图片")
    public CommonResult<UploadFile> uploadImage(@RequestParam("iFile") MultipartFile file) {
        try {
            UploadFile responseParam = uploadService.uploadImage(file);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("图片上传失败：" + e.getMessage());
        }
    }

    /**
     * 上传文件
     */
    @PostMapping("/file")
    @Operation(summary = "上传文件")
    public CommonResult<UploadResponseParam> uploadFile(@RequestParam("iFile") MultipartFile file) {
        try {
            UploadResponseParam responseParam = uploadService.uploadFile(file);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("文件上传失败：" + e.getMessage());
        }
    }

    /**
     * 批量上传图片
     */
    @PostMapping("/images")
    @Operation(summary = "批量上传图片")
    public CommonResult<UploadResponseParam> uploadImages(@RequestParam("iFiles") MultipartFile[] files,
                                                          @RequestParam(required = false) String token,
                                                          @RequestParam(required = false) Long wxappId) {
        try {
            UploadResponseParam responseParam = uploadService.uploadImages(files, token, wxappId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("批量上传图片失败：" + e.getMessage());
        }
    }

    /**
     * 删除文件
     */
    @PostMapping("/delete")
    @Operation(summary = "删除文件")
    public CommonResult<String> deleteFile(@RequestParam Long fileId,
                                          @RequestParam(required = false) String token) {
        try {
            uploadService.deleteFile(fileId);
            return CommonResult.ok("删除成功");
        } catch (Exception e) {
            return CommonResult.error("删除文件失败：" + e.getMessage());
        }
    }

    /**
     * 获取文件信息
     */
    @GetMapping("/info/{fileId}")
    @Operation(summary = "获取文件信息")
    public CommonResult<UploadResponseParam> getFileInfo(@PathVariable String fileId,
                                                         @RequestParam(required = false) String token) {
        try {
            UploadResponseParam responseParam = uploadService.getFileInfo(fileId, token);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取文件信息失败：" + e.getMessage());
        }
    }

    /**
     * 检查文件是否存在
     */
    @GetMapping("/check")
    @Operation(summary = "检查文件是否存在")
    public CommonResult<UploadResponseParam> checkFile(@RequestParam String filePath,
                                                       @RequestParam(required = false) String token) {
        try {
            UploadResponseParam responseParam = uploadService.checkFile(filePath, token);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("检查文件失败：" + e.getMessage());
        }
    }
} 